package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class ExamManagePage extends Utils {
    public static String url = "http://dongliyang.online:10030/oj/layout/exam";

    public ExamManagePage() throws IOException, InterruptedException {
        super(url);
    }

    // 未登录状态 - 验证是否重定向到登录页面
    public void pageRightWithoutLogin() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 导航到竞赛管理页面
        driver.get(url);

        // 等待重定向到登录页面
        wait.until(ExpectedConditions.urlContains("/oj/login"));
        assert driver.getCurrentUrl().contains("/oj/login") : "未登录状态下未重定向到登录页面";
        System.out.println("未登录状态下访问竞赛管理页面，重定向到登录页面，当前 URL: " + driver.getCurrentUrl());
        getScreenShot("examManagePageWithoutLogin");
    }

    // 已登录状态 - 验证页面加载
    public void pageRightWithLogin() throws IOException {
        // 导航到竞赛管理页面
        driver.get(url);
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 等待页面加载完成（检查表格是否存在）
        wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[2]/table/thead/tr/th[1]/div")
        ));

        System.out.println("已登录状态下竞赛管理页面加载成功。");
        getScreenShot("examManagePageWithLogin");
    }

    // 登录用户检查 - 验证当前用户
    public void checkCurrentUser() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 定位右上角用户名
        WebElement userElement = wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/header/div/span/div")
        ));
        String userText = userElement.getText().trim();
        String expectedUser = "当前用户：超级管理员";
        assert userText.equals(expectedUser) : "当前用户不正确，预期: " + expectedUser + ", 实际: " + userText;

        System.out.println("当前用户验证成功: " + userText);
        getScreenShot("examManagePageCheckUser");
    }

    // 竞赛标题检查 - 验证是否在竞赛管理页面
    public void checkPageTitle() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 验证表格列名（第一列应为“竞赛标题”）
        WebElement headerElement = wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[2]/table/thead/tr/th[1]/div")
        ));
        String headerText = headerElement.getText().trim();
        assert headerText.equals("竞赛标题") : "页面标题不正确，预期: 竞赛标题, 实际: " + headerText;

        System.out.println("竞赛管理页面标题验证成功: " + headerText);
        getScreenShot("examManagePageCheckTitle");
    }

    // 搜索功能 - 根据竞赛标题搜索并验证结果
    public void searchExam(String examTitle) throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 定位搜索框并输入竞赛标题
        WebElement searchInput = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/form/div[2]/div/div/div/input")
        ));
        searchInput.clear();
        searchInput.sendKeys(examTitle);

        // 定位并点击搜索按钮
        WebElement searchButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/form/div[3]/div/button[1]")
        ));
        searchButton.click();

        // 等待搜索结果加载（检查表格第一行第一列是否包含竞赛标题）
        wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[4]/td[1]/div")
        ));
        WebElement firstRowExamTitle = driver.findElement(By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[4]/td[1]/div"));
        String actualExamTitle = firstRowExamTitle.getText().trim();
        assert actualExamTitle.equals(examTitle) : "搜索结果不正确，预期竞赛标题: " + examTitle + ", 实际: " + actualExamTitle;

        System.out.println("成功搜索竞赛标题: " + examTitle);
        getScreenShot("searchExam_" + examTitle);
    }

    // 添加竞赛
    public void addExam() throws IOException, InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 定位“添加竞赛”按钮
        WebElement addButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/form/div[3]/div/button[3]")
        ));
        addButton.click();

//        // 等待添加页面加载（假设跳转到一个添加表单）
//        wait.until(ExpectedConditions.presenceOfElementLocated(
//                By.xpath("//form[contains(., '竞赛标题')]")
//        ));

        // 输入竞赛标题
        WebElement titleInput = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[1]/div[2]/div[1]/div/div[2]/div/div/input")
        ));
        String newTitle = "New Exam " + System.currentTimeMillis();
        titleInput.sendKeys(newTitle);

//        // 输入创建时间（假设有创建时间输入框）
//        WebElement createTimeInput = wait.until(ExpectedConditions.elementToBeClickable(
//                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[1]/div[2]/div[2]/div/div[2]/div/input[1]")
//        ));
//        createTimeInput.sendKeys("2025-04-12 00:00:00");

        // 输入开始时间
        WebElement startTimeInput = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[1]/div[2]/div[2]/div/div[2]/div/input[1]")
        ));
        startTimeInput.sendKeys("2025-04-18 00:00:00");

        // 输入结束时间
        WebElement endTimeInput = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[1]/div[2]/div[2]/div/div[2]/div/input[2]")
        ));
        endTimeInput.sendKeys("2025-04-19 00:00:00");

        // 额外操作
        WebElement Button = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/header")
        ));

        Button.click();

//        /html/body/div[1]/div/section/main/div[2]/div/div/div[1]/div[2]/div[3]/div/button

        // 保存基本信息
        WebElement saveButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[1]/div[2]/div[3]/div/button")
        ));

        saveButton.click();
        Thread.sleep(1000);

//        /html/body/div[1]/div/section/main/div[2]/div/div/div[2]/button
        // 提交添加
        WebElement addBasicButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[2]/button")
        ));
        addBasicButton.click();
//        Thread.sleep(3000);

        // 提交添加
        WebElement addQuestionButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[3]/div/div/div/div/div/div[2]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[1]/div/label/span/span")
        ));
        addQuestionButton.click();

        WebElement submitButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[3]/div/div/div/div/div/div[3]/button")
        ));
        submitButton.click();

        //
        wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[4]/button[2]")
        )).click();

        // 验证新竞赛是否添加成功
        WebElement newExamTitle = driver.findElement(By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[1]"));
        String actualTitle = newExamTitle.getText().trim();
        assert actualTitle.equals(newTitle) : "新竞赛未正确添加，预期: " + newTitle + ", 实际: " + actualTitle;

        System.out.println("成功添加新竞赛，竞赛标题: " + actualTitle);
        getScreenShot("addExam");
    }

    // 编辑第一个竞赛
    public void editFirstExam() throws IOException, InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        // 定位第一个竞赛的编辑按钮（操作列是最后一列）
        WebElement goBackButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[8]/div/button")
        ));
        goBackButton.click();
        System.out.println("撤销发布成功");
        Thread.sleep(500);


        // 定位第一个竞赛的编辑按钮（操作列是最后一列）
        WebElement editButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[8]/div/button[1]")
        ));
        editButton.click();

//        // 等待编辑页面加载
//        wait.until(ExpectedConditions.presenceOfElementLocated(
//                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[1]/div[1]/span[1]")
//        ));

        // 修改竞赛标题
        WebElement titleInput = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[1]/div[2]/div[1]/div/div[2]/div/div/input")
        ));
        String newTitle = "Updated Exam " + System.currentTimeMillis();
        titleInput.clear();
        titleInput.sendKeys(newTitle);

        // 提交编辑
        WebElement submitButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[1]/div[2]/div[3]/div/button")
        ));
        submitButton.click();
        // 返回
        WebElement returnButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div/div/div[1]/div[1]/span[2]")
        ));
        returnButton.click();

        // 等待返回竞赛管理页面
        wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[2]/table/thead/tr/th[1]/div")
        ));

        // 验证竞赛标题是否更新
        WebElement updatedTitle = driver.findElement(By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[1]/div"));
        String actualTitle = updatedTitle.getText().trim();
        assert actualTitle.equals(newTitle) : "竞赛标题未正确更新，预期: " + newTitle + ", 实际: " + actualTitle;

        System.out.println("成功编辑第一个竞赛，更新后的竞赛标题: " + actualTitle);
        getScreenShot("editFirstExam");
    }

    // 删除第一个竞赛
    public void deleteFirstExam() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 记录第一个竞赛的标题
        WebElement firstExamTitle = wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[1]/div")
        ));
        String examTitle = firstExamTitle.getText().trim();

        // 定位第一个竞赛的删除按钮
        WebElement deleteButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[8]/div/button[2]")
        ));
        deleteButton.click();

//        // 等待确认弹窗
//        WebElement confirmButton = wait.until(ExpectedConditions.elementToBeClickable(
//                By.xpath("//button[contains(., '确定')]")
//        ));
//        confirmButton.click();

        // 等待表格更新
        wait.until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("//table/tbody/tr[1]/td[1]")
        ));

        // 验证竞赛是否删除
        WebElement newFirstExamTitle = driver.findElement(By.xpath("/html/body/div[1]/div/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[1]"));
        String newExamTitle = newFirstExamTitle.getText().trim();
        assert !newExamTitle.equals(examTitle) : "竞赛未正确删除，预期标题不等于: " + examTitle + ", 实际: " + newExamTitle;

        System.out.println("成功删除竞赛标题: " + examTitle);
        getScreenShot("deleteFirstExam");
    }

//    // 撤销删除
//    public void undoDelete() throws IOException {
//        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
//
//        // 记录当前第一个竞赛的标题
//        WebElement firstExamTitle = wait.until(ExpectedConditions.presenceOfElementLocated(
//                By.xpath("//table/tbody/tr[1]/td[1]")
//        ));
//        String beforeUndoTitle = firstExamTitle.getText().trim();
//
//        // 定位撤销按钮（假设页面有一个撤销按钮）
//        WebElement undoButton = wait.until(ExpectedConditions.elementToBeClickable(
//                By.xpath("//button[contains(., '撤销')]")
//        ));
//        undoButton.click();
//
//        // 等待表格更新
//        wait.until(ExpectedConditions.presenceOfElementLocated(
//                By.xpath("//table/tbody/tr[1]/td[1]")
//        ));
//
//        // 验证竞赛是否恢复
//        WebElement newFirstExamTitle = driver.findElement(By.xpath("//table/tbody/tr[1]/td[1]"));
//        String afterUndoTitle = newFirstExamTitle.getText().trim();
//        assert !afterUndoTitle.equals(beforeUndoTitle) : "撤销删除失败，预期标题不等于: " + beforeUndoTitle + ", 实际: " + afterUndoTitle;
//
//        System.out.println("成功撤销删除，恢复的竞赛标题: " + afterUndoTitle);
//        getScreenShot("undoDeleteExam");
//    }

    // 综合测试
    public void testExamManagePage() throws IOException, InterruptedException {
        // 未登录状态测试
//        pageRightWithoutLogin();

        // 已登录状态测试
        pageRightWithLogin();

        // 检查当前用户
        checkCurrentUser();

        // 检查页面标题
        checkPageTitle();

        // 搜索竞赛
        String examTitle = "44"; // 从截图中获取一个竞赛标题
        searchExam(examTitle);

        // 添加竞赛
        addExam();

        // 编辑第一个竞赛
        editFirstExam();

        // 删除第一个竞赛
        deleteFirstExam();

        // 撤销删除
//        undoDelete();
    }
}